import { Label } from "cc"
import { _decorator, Component, Node } from "cc"
const { ccclass, property } = _decorator

@ccclass("Reward")
export class Reward extends Component {
  @property
  eachHp: number = 0

  @property(Label)
  labCurrHp: Label = null!

  @property([Node])
  arrNodeWall: Node[] = []

  private currHp:number = 0

  private maxHp:number = 0

  private indexHp:number = 0

  private maxIndexHp:number = 0

  protected _isDie: boolean = false

  public set isDie(v: boolean) {
    this._isDie = v

    if (this._isDie) {
      this.showReward()
    }
  }
  public get isDie() {
    return this._isDie
  }

  start() {
    this.init()
  }

  init() {
    this.maxHp = this.eachHp * this.arrNodeWall.length
    this.maxIndexHp = this.arrNodeWall.length
    this.indexHp = this.arrNodeWall.length - 1
    this.currHp = this.maxHp
    this.onChangeHp()
  }

  update(deltaTime: number) {}

  onBeAttack(attack: number) {
    if (this.isDie) return
    this.changeHp(attack)
  }

  private showReward() {}

  private changeHp(attack: number) {
    // if(this.isDie) return
    // this.currHp -= attack
    // this.currHp = Math.max(this.currHp,0)
    // if(this.currHp < this.indexHp / this.maxIndexHp * this.maxHp){
    //   console.log("--------")
    //   this.arrNodeWall[0].destroy()
    //   this.arrNodeWall.splice(0,1)
    //   this.indexHp--

    //   for(){}
    // }
    // for(let i:number = 0;i < this.arrNodeWall.length;i++){
    //   if(i / this.arrNodeWall.length >= this.currHp / this.maxHp){
    //     const node = this.arrNodeWall[i]
    //     if(node && node.isValid){
    //       node.destroy()
    //     }
    //   }
    // }

    this.onChangeHp()

  }

  private onChangeHp(){
    this.labCurrHp.string = this.currHp + ""
  }
}
